package sis.ra.crawlingInfobox;

import java.util.ArrayList;

import sis.ra.utility.Utils;


public class crawlingProducts {

	public static String crawlInfoBoxPartCompany(String wikidoc)
	{
		String infobox="";
		if (wikidoc.length()>10) 
		{	String[] lines=wikidoc.split("\n");
			for (int i=0;i<lines.length;i++)
			{
//				System.out.println(lines[i]);
				if (lines[i].toLowerCase().contains("{{infobox")&&lines[i].toLowerCase().contains("company"))
				{
					while (!lines[i].equals("}}")&&!lines[i].trim().startsWith("}}")&&!lines[i].equals("|}}"))
					{
//						System.out.println(lines[i]);
						if (lines[i].startsWith("|"))
							infobox=infobox+"| "+lines[i].substring(1).trim()+" ";
						else
							infobox=infobox+lines[i].trim()+" ";
						i=i+1;
					}
					break;
				}
			}		
		}
		return infobox;
	}
	
	public static String getInterPagePart(String companyname, String sectionname)
	{
		String section="";
		if (sectionname.contains("|"))
		{
			sectionname=sectionname.substring(0,sectionname.indexOf("|"));
		}
		String wikipage="http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=API|"+companyname.replace(" ", "%20")+"&rvprop=content&format=xml";
		String wikicontent=Utils.fetchContentfrURL(wikipage);
	//	System.out.println(wikicontent);
	//	System.out.println(sectionname+" "+sectionname);
		String [] lines=wikicontent.split("\n");
		for (int i=0;i<lines.length;i++)
		{
	//		System.out.println(sectionname+" "+lines[i]);
			if (lines[i].startsWith("=")&&lines[i].toLowerCase().contains(sectionname.toLowerCase()))
			{
				section=section+lines[i]+"\n";
				i=i+1;
				while (!lines[i].startsWith("="))
				{
					section=section+lines[i]+"\n";
					i=i+1;
				}
			}
		}
		return section;
	}
	
	public static ArrayList<String> extractProcductsFrmInfoBox(String infoboxcompany, String companyname)
	{
		ArrayList<String> productsarray=new ArrayList<String>();
		String products="";
		String [] lines=infoboxcompany.split("\\| ");
		for (int i=0;i<lines.length;i++)
		{
	//		System.out.println(lines[i]);
			if (lines[i].trim().startsWith("products"))
			{
	//			System.out.println(lines[i]);
			//[[Zune]]<ref>[http://www.zune.net/en-US/ zune.net]</ref>
				products=lines[i].trim().substring(8).replace("=", "").replace("&lt;br&gt;", "").replace("&lt;br /&gt;", "").trim();
//				System.out.print(products);
			}
		}
		
		String [] pros=products.split("]]");
		for (int j=0;j<pros.length;j++)
		{
//			System.out.println(pros[j]);
			if (!pros[j].contains("[[")) continue;
			if (pros[j].contains("list of")) 
			{
				String pro=pros[j].substring(pros[j].indexOf("[[")+2);
				if (pro.startsWith("#"))
				{
					String part=getInterPagePart(companyname,pro.substring(1));
					if (part.contains("{{main"))
					{
						String thislink=part.substring(part.indexOf("{{main"),part.indexOf("}}"));
						thislink=thislink.substring(thislink.indexOf("|")+1);
						if (thislink.toLowerCase().contains("list of"))
						{
							productsarray=crawlingListOfProducts.getListProducts(thislink);
							return productsarray;
		//					System.out.println(content);
						}
					}
				}
			}
			String pro=pros[j].substring(pros[j].indexOf("[[")+2);
	//		System.out.println("something: "+pro);
			productsarray.add(pro);			
		}
		return productsarray;
	}
	
	public static void main(String[] args) {
		String companynames="Google;Amazon.com;Liberty Media;eBay;Yahoo!;Microsoft;Oracle Corporation;Symantec;SAP AG;ExxonMobil;Chevron Corporation;ConocoPhillips;Valero Energy Corporation;Marathon Oil;Sunoco;Hess Corporation;Tesoro;Murphy Oil;Western Refining;Frontier Oil";
		
		String [] companys=companynames.split(";");
		
		for (int i=0;i<companys.length;i++)
		{
			String companyname=companys[i];
//			String companyname="Microsoft";
			String wikipage="http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=API|"+companyname.replace(" ", "%20")+"&rvprop=content&format=xml";
			String wikicontent=Utils.fetchContentfrURL(wikipage);
			String infoboxcompany=crawlInfoBoxPartCompany(wikicontent);
			System.out.print(companyname+"\t");
//			System.out.println("company: "+infoboxcompany);
			ArrayList<String> products=new ArrayList<String> ();
			if (infoboxcompany.length()>2)
			{
				products=extractProcductsFrmInfoBox(infoboxcompany,companyname.replace(" ", "%20"));
			}
			System.out.print(products.size()+"\t");
			for (int j=0;j<products.size();j++)
			{
				if (products.get(j).toLowerCase().contains("list of"))
				{
					String product=crawlingListOfProducts.toString(crawlingListOfProducts.getListProducts(products.get(j)));
					System.out.print(product);
				}else
				System.out.print(products.get(j)+" ; ");
			}
			System.out.println();
		}
	}

}
